﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
using System.Windows.Forms;

namespace Out2Share
{
    public partial class ThisAddIn
    {
        /// <summary>
        /// Store treeview across refresh. Used in SharepointConnect class to set its content 
        /// </summary>
        public static TreeViewContainer treeViewContainer = new TreeViewContainer();
        public static Outlook.Application app = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            app = this.Application;
        }

        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        public static void GetApplication(string flag, string category) {
            try
            {
                Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI");
                Microsoft.Office.Interop.Outlook.MAPIFolder objFolder = NS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
                Microsoft.Office.Interop.Outlook.MailItem objMail;
                Microsoft.Office.Interop.Outlook.Items oItems;
                oItems = objFolder.Items;
                //MessageBox.Show("Reading mails");
                for (int i = 1; i <= app.ActiveExplorer().Selection.Count; i++)
                {
                    //MessageBox.Show("Reading :" + i.ToString());
                    objMail = (Microsoft.Office.Interop.Outlook.MailItem)app.ActiveExplorer().Selection[i];
                    //MessageBox.Show(objMail.Body.ToString());

                    if (flag != "")
                    {
                        objMail.FlagRequest = flag;
                    }
                    if (category != "")
                    {
                        if (objMail.Categories == null) // no current categories assigned
                            objMail.Categories = category;
                        else if (!objMail.Categories.Contains(category)) // insert as first assigned category
                            objMail.Categories = string.Format("{0}, {1}", category, objMail.Categories);
                    }
                    objMail.Save();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                //NS.Logoff();
                //objFolder = null;
                //objMail = null;
                //app = null;
            }

        
        }

        #region VSTO generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
        
        #endregion
    }
}
